package com.commons.utils;

import java.util.Calendar;
import java.util.Date;
public class DateInterval {
	Date begin;
	Date end;
	
	private DateInterval()
	{
		
	}
	
	public static DateInterval getInstanceToday()
	{
		DateInterval di=new DateInterval();
		Calendar c=Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0);
		di.setBegin(c.getTime());
		
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)+1,0,0,0);
		di.setEnd(c.getTime());
		return di;
	}
	
	public static DateInterval getInstanceWeek()
	{
		DateInterval di=new DateInterval();
		Calendar c=Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),0,0,0);
		if(c.get(Calendar.DAY_OF_WEEK)==1)
		{
			c.roll(Calendar.DAY_OF_YEAR,1);
			di.setEnd(c.getTime());
			
			di.setBegin(new Date(c.getTime().getTime()-1000*60*60*24*7));
		}
		else
		{
			c.roll(Calendar.DAY_OF_YEAR,-(c.get(Calendar.DAY_OF_WEEK)-2));
			di.setBegin(c.getTime());
		
			di.setEnd(new Date(c.getTime().getTime()+1000*60*60*24*7));
		}
		return di;
	}
	
	public static DateInterval getInstanceMonth()
	{
		DateInterval di=new DateInterval();
		Calendar c=Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),1,0,0,0);
		di.setBegin(c.getTime());
		
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1,1,0,0,0);
		di.setEnd(c.getTime());
		return di;
	}
	
	

	public Date getBegin() {
		return begin;
	}

	public void setBegin(Date begin) {
		this.begin = begin;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}
	
}
